//
//  LoopComposePicCell.swift
//  LoopWeibo
//
//  Created by loop安 on 2016/12/17.
//  Copyright © 2016年 loop. All rights reserved.
//

import UIKit

@objc protocol LoopComposePicCellDelegate:NSObjectProtocol {
    @objc optional func userWillChoosePic(cell:LoopComposePicCell?)
    @objc optional func userWillRemovePic(cell:LoopComposePicCell)
}

class LoopComposePicCell: UICollectionViewCell {
    
    weak var delegate:LoopComposePicCellDelegate?
    
    var image:UIImage?{
    
        didSet{
            addBtn.setImage(image, for: .normal)
            closeBtn.isHidden = image == nil
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        addSubview(addBtn)
        addSubview(closeBtn)
//        closeBtn.isHidden = true
        
        addBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        closeBtn.snp.makeConstraints { (make) in
            make.top.trailing.equalTo(self)
        }
        
        addBtn.addTarget(self, action: #selector(clickAddBtn), for: .touchUpInside)
        closeBtn.addTarget(self, action: #selector(clickCloseBtn), for: .touchUpInside)
        
    }
    
    @objc private func clickAddBtn(){
        print(#function)
        if image != nil {
        
            return
        }
        
        delegate?.userWillChoosePic?(cell: self)
    }
    
    @objc private func clickCloseBtn(){
        print(#function)

        delegate?.userWillRemovePic?(cell: self)
    }
    
    let addBtn:UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(#imageLiteral(resourceName: "compose_pic_add"), for: .normal)
        btn.contentMode = .scaleAspectFill
        return btn
    }()
    
    let closeBtn:UIButton = {
    
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "compose_photo_close"), for: .normal)
        btn.sizeToFit()
        return btn
    }()
    
}


